/**
 * 
 */
package com.adcc.monitoragent.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Gzip工具类
 * @author charlie
 *
 */
public class GzipUtil {
	
	/**
	 * 对象转换成字节数组并gzip压缩
	 * @param obj 对象
	 * @return 字节数组
	 */
	public final static byte[] objToBytesGzip(Object obj) {
		ByteArrayOutputStream bao = null;
		ObjectOutputStream oos = null;
		GZIPOutputStream gzout = null;
		try {
			bao = new ByteArrayOutputStream();
			//建立gzip压缩输出流 
			gzout=new GZIPOutputStream(bao); 
			oos = new ObjectOutputStream(gzout);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
			oos = null;
			return bao.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if(oos != null) {
					oos.close();
					oos = null;
				}
				if(gzout != null) {
					gzout.close();
					gzout = null;
				}
				if (bao != null) {
					bao.close();
					bao = null;
				}
			} catch (IOException e) {
			}
		}
	}

	/**
	 * 字节数组转成对象，并gzip压缩
	 * @param bytes
	 * @return
	 */
	public final static Object bytesToObjGzip(byte[] bytes) {
		ByteArrayInputStream bai = null;
		GZIPInputStream gzin = null;
		ObjectInputStream ois = null;
		try {
			bai = new ByteArrayInputStream(bytes);
			gzin=new GZIPInputStream(bai); 
			ois = new ObjectInputStream(gzin);
			Object obj = ois.readObject();
			ois.close();
			ois = null;
			return obj;
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		} finally {
			try {
				if (bai != null) {
					bai.close();
					bai = null;
				}
				if (gzin != null) {
					gzin.close();
					gzin = null;
				}
				if (ois != null) {
					ois.close();
					ois = null;
				}
			} catch (IOException e) {
			}
		}
	}
	
	/**
	 * gzip压缩字节数组
	 * @param buffer
	 * @return
	 * @throws IOException
	 */
	public static byte[] compress(byte[] buffer) {
		ByteArrayOutputStream arrayOutputStream = null;
		GZIPOutputStream gzip = null;
		ByteArrayInputStream inputStream = null;

		try {
			arrayOutputStream = new ByteArrayOutputStream();
			gzip = new GZIPOutputStream(arrayOutputStream);
			inputStream = new ByteArrayInputStream(buffer);
			byte[] buf = new byte[2048];
			int len = 0;
			while ((len = inputStream.read(buf)) != -1) {
				gzip.write(buf, 0, len);
			}

			gzip.finish();

			byte[] result = arrayOutputStream.toByteArray();

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return buffer;
		} finally {
			if(gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(arrayOutputStream != null) {
				try {
					arrayOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * gzip解压字节数组
	 * @param buffer
	 * @return
	 * @throws IOException
	 */
	public static byte[] decompress(byte[] buffer) {
		ByteArrayInputStream inputStream = null;
		// Open the compressed stream
		GZIPInputStream gzip = null;

		ByteArrayOutputStream out = null;

		try {
			inputStream = new ByteArrayInputStream(buffer);
			gzip = new GZIPInputStream(inputStream);
			out = new ByteArrayOutputStream();
			// Transfer bytes from the compressed stream to the output stream
			byte[] buf = new byte[2048];
			int len;
			while ((len = gzip.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			return buffer;
		} finally {
			// Close the file and stream
			if(gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
